package com.lgkj.adapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cb.ratingbar.CBRatingBar;
import com.lgkj.R;
import com.lgkj.bean.HairStyleBean;
import com.lgkj.ui.ImageBrowseActivity;
import com.lgkj.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/3 0003.
 */

public class HairStyleAdapter extends BaseAdapter implements View.OnClickListener {
    private static final String TAG = "HairStyleAdapter";
    private Context context;

    private List<HairStyleBean.DataBean> data;

    private HairSalonAdapter.MyClickListener mListener;

    public void setClick(HairSalonAdapter.MyClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View view) {
        mListener.clickListener(view);
    }


    public void setData(List<HairStyleBean.DataBean> datas) {
        if (datas != null) {
            this.data = datas;
        }
    }

    public HairStyleAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return data.size() + 1;
    }

    @Override
    public HairStyleBean.DataBean getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.hair_style_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data.size() == 0) {
//            viewHolder.ivPhoto.setImageResource(R.mipmap.trends_add);
//            viewHolder.ivPhoto.setScaleType(ImageView.ScaleType.CENTER);
//            viewHolder.ivPhoto.setVisibility(View.GONE);
            viewHolder.tv_haop.setVisibility(View.GONE);
            viewHolder.rbFavour.setVisibility(View.GONE);
            viewHolder.flItem.setBackgroundResource(R.mipmap.sytle_item_add_bg);
        } else {
            if (i < data.size()) {
                viewHolder.rbFavour.setVisibility(View.VISIBLE);
                viewHolder.tv_haop.setVisibility(View.VISIBLE);
                viewHolder.flItem.setBackgroundColor(context.getResources().getColor(R.color.item_colo));
                viewHolder.ibDel.setVisibility(View.VISIBLE);
                viewHolder.ibDel.setOnClickListener(this);
                viewHolder.ivPhoto.setScaleType(ImageView.ScaleType.FIT_XY);
                viewHolder.ibDel.setTag(i);
                viewHolder.ibDel.setVisibility(View.VISIBLE);
                viewHolder.tvName.setText(data.get(i).getBarber_name());
                viewHolder.tvDaoshi.setText(data.get(i).getBarber_name());
                viewHolder.rbFavour.setStarCount(5);
                CommonUtils.LoadImage(context, viewHolder.ivPhoto, data.get(i).getBarber_img(), 0);
                viewHolder.ivPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Activity activity = (Activity) context;
                        String url = data.get(i).getBarber_img();
                        Intent intent = new Intent(context, ImageBrowseActivity.class);
                        intent.putExtra("image_url", url);
                        context.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, view, "shareNames").toBundle());
                    }
                });

            } else {
                Log.i(TAG, "getView: " + i);
                viewHolder.tv_haop.setVisibility(View.GONE);
                viewHolder.rbFavour.setVisibility(View.GONE);
                viewHolder.ibDel.setVisibility(View.GONE);
                viewHolder.flItem.setBackgroundResource(R.mipmap.sytle_item_add_bg);
            }
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.fl_item)
        FrameLayout flItem;
        @BindView(R.id.ib_del)
        ImageButton ibDel;
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_daoshi)
        TextView tvDaoshi;
        @BindView(R.id.tv_haop)
        TextView tv_haop;
        @BindView(R.id.rb_favour)
        CBRatingBar rbFavour;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private View.OnClickListener goBigIamge = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Activity activity = (Activity) context;
            int i=Integer.parseInt(view.getTag().toString());
            String url = data.get(i).getBarber_img();
            Intent intent = new Intent(context, ImageBrowseActivity.class);
            intent.putExtra("image_url", url);
            context.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, view, "shareNames").toBundle());
        }
    };
}
